package com.hnkywl.sts.dao.zjgl;

import com.hnkywl.sts.entity.cwgl.kmys.Cyfyys;
import com.hnkywl.sts.entity.zjgl.Jgzs;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2x.StringUtils;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.stereotype.Repository;


import java.util.ArrayList;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by HL on 2015/9/18.
 */
@Repository
public class JgzsDao extends SimpleHibernateTemplate<Jgzs, Long> {


   /* public Page<Jgzs> findAllJgzs( int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Jgzs> page = new Page<Jgzs>(pageNo, pageSize, 0);
        //==============条件拼接===============

      *//*  if (kmdm != null && !kmdm.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" kmdm like ? ");
            param.add("%"+kmdm.trim()+"%");
        }
        if (gsId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" nbgs.id = ? ");
            param.add(gsId);
        }*//*
        //==============记录数量===============
        hql = "select count(*) from Jgzs where 1=1" ;
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Jgzs where 1=1" ;
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }
*/


    public Page<Jgzs> findAllJgzs(int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Jgzs> page = new Page<Jgzs>(pageNo, pageSize, 0);

        //==============记录数量===============
        hql = "select count(*) from Jgzs where deleted !=1";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Jgzs  where deleted !=1";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        hql = hql + " order by jgrq desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }


    public Boolean checkUnique(Long id, String propertyName, Object propertyValue) {
        String hql;
        if (propertyValue == null)
            return false;
        if (id == null) {
            //修改检验
            hql = "from Jgzs where " + propertyName + " = ? ";
            return this.existsQuery(hql, propertyValue);
        } else {
            //新增检验
            hql = "from Jgzs where " + propertyName + " = ? and id <>?";
            return this.existsQuery(hql, propertyValue, id);
        }
    }


    public Jgzs findJgzsrq() {
        String sql = "select jgrq from t_biz_jgzs jgzs where rownum = 1 order by jgrq ";
        return (Jgzs) findUnique(sql);
    }


//    public Jgzs findJgzsByJgrq(Date jgrq) {
//        return findUniqueByProperty("jgrq", jgrq) == null ? new Jgzs() : findUniqueByProperty("jgrq", jgrq);
//    }

    public Jgzs findJgzsByJgrq(Date jgrq) {
        Jgzs jgzs = new Jgzs();
        String sql = "select j.id from t_biz_jgzs j where j.deleted = 0 and j.jgrq = trunc(?)";
        List<Map> list = (List<Map>) this.createSQLQuery(sql, jgrq).setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE).list();
        if (list.size() > 0) {
            jgzs = this.get(Long.valueOf(list.get(0).get("ID").toString()));
        }
        return jgzs;
    }


}
